import path from 'path'
import fs from 'fs/promises'
import { glob } from 'glob'

const src = 'D:/研判知识库/pdf/papers/**/*.pdf'
const targetDir = 'D:/研判知识库/提取结果/papers'
const pendingDir = 'D:/研判知识库/pdf/papers-pending'

const files = await glob(src)
const pending = []

// 预先获取目标目录所有文件
const targetFiles = await glob(path.join(targetDir, '*.*').replace(/\\/g, '/'))
const targetFileSet = new Set(targetFiles.map(file => path.basename(file, path.extname(file))))

for (const filePath of files) {
  const { name } = path.parse(filePath)

  if (!targetFileSet.has(name)) {
    pending.push(filePath)
  }
}

// 创建目标文件夹（如果不存在）
await fs.mkdir(pendingDir, { recursive: true })

// 复制所有待处理文件
console.log(`找到 ${pending.length} 个待处理文件`)
for (const filePath of pending) {
  const fileName = path.basename(filePath)
  const targetPath = path.join(pendingDir, fileName)

  try {
    await fs.copyFile(filePath, targetPath)
    console.log(`已复制: ${fileName}`)
  } catch (error) {
    console.error(`复制失败 ${fileName}:`, error.message)
  }
}

console.log('所有待处理文件已复制完成！')
